package com.xbongbong.paas.controller.process;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.vo.AfterSaveVO;
import com.xbongbong.paas.service.process.DistributorProcessDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.process.pojo.dto.ProcessSaveDataDTO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/07/19 16:15
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/process")
public class ProcessDataController {

    @Resource
    private DistributorProcessDataService distributorProcessDataService;

    /**
     * 经销商，流程审批通过插入主数据
     *
     * @param processSaveDataDTO 入参
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2022/7/19 16:21
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/distributor/formDataAdd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formDataAdd(@RequestBody @Valid ProcessSaveDataDTO processSaveDataDTO, BindingResult br) {
        XbbResponse<AfterSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AfterSaveVO afterSaveVO = distributorProcessDataService.insert(processSaveDataDTO.getFormDataAddDTO(), processSaveDataDTO.getPaasFormDataEntity());
                response = new XbbResponse<>(afterSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 经销商，流程审批通过更新主数据
     *
     * @param processSaveDataDTO 入参
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2022/7/19 16:21
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/distributor/formDataUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formDataUpdate(@RequestBody @Valid ProcessSaveDataDTO processSaveDataDTO, BindingResult br) {
        XbbResponse<AfterSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AfterSaveVO afterSaveVO = distributorProcessDataService.update(processSaveDataDTO.getFormDataUpdateDTO(), processSaveDataDTO.getPaasFormDataEntity(), processSaveDataDTO.getOldFormData());
                response = new XbbResponse<>(afterSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
